/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.xiao.service.impl;

import me.zhengjie.modules.xiao.domain.News;
import me.zhengjie.modules.xiao.domain.dto.NewsDTO;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.xiao.service.NewsService;
import me.zhengjie.modules.xiao.domain.vo.NewsQueryCriteria;
import me.zhengjie.modules.xiao.mapper.NewsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author cg
* @date 2025-04-12
**/
@Service
@RequiredArgsConstructor
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    private final NewsMapper newsMapper;

    @Override
    public PageResult<News> queryAll(NewsQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(newsMapper.findAll(criteria, page));
    }

    @Override
    public List<News> queryAll(NewsQueryCriteria criteria){
        return newsMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(News resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(News resources) {
        News news = getById(resources.getId());
        news.copy(resources);
        saveOrUpdate(news);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<News> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (News news : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("姓名", news.getName());
            map.put("手机号", news.getPhone());
            map.put("状态", news.getFlag());
            map.put("创建者", news.getCreateBy());
            map.put("更新者", news.getUpdateBy());
            map.put("创建日期", news.getCreateTime());
            map.put("更新时间", news.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<NewsDTO> top5(NewsQueryCriteria criteria){
        List<News> listEntity = newsMapper.top5(criteria);
        List<NewsDTO> listDTO = new ArrayList<>();
        for(News news:listEntity){
            NewsDTO dto = new NewsDTO();
            dto.setName(news.getName());
            //手机号保留前三位 后四位 中间的用*
            dto.setPhone(news.getPhone().substring(0,3)+"****"+news.getPhone().substring(7,11));
            if(news.getFlag().equals("1")){
                dto.setFlagStr("申请了免费设计");
            }else if(news.getFlag().equals("2")){
                dto.setFlagStr("申请了免费看房");
            }else if(news.getFlag().equals("3")){
                dto.setFlagStr("申请了免费报价");
            }

            String time = news.getCreateTime().toString();
            dto.setDataStr(time.substring(0,time.indexOf(".")));
            //根据创建时间 7天前就是一周前 14天前是2周前 21天前是3周前 7天以内就按天说
//            long createTimestamp = news.getCreateTime().getTime();
//            long now = System.currentTimeMillis();
//            // createTimestamp 算出时分秒 hh:mm:ss
//            String[] split = news.getCreateTime().toString().split(" ");
//            String[] split1 = split[1].split(":");
//            String shi = split1[0]+":"+split1[1]+":"+split1[2].substring(0,split1[2].indexOf("."));
//            if(now-createTimestamp<24*60*60*1000){
//                dto.setDataStr("今天 "+shi);
//            }else if(now-createTimestamp<7*24*60*60*1000){
//                dto.setDataStr(String.valueOf((System.currentTimeMillis()-news.getCreateTime().getTime())/(24*60*60*1000))+"天前");
//            }else if(now-createTimestamp<14*24*60*60*1000){
//                dto.setDataStr("1周前 "+shi);
//            }else if (now-createTimestamp<21*24*60*60*1000){
//                dto.setDataStr("2周前 "+shi);
//            }else if(now-createTimestamp<28*24*60*60*1000){
//                dto.setDataStr("3周前 "+shi);
//            }else{
//                dto.setDataStr("1月前 "+shi);
//            }
            listDTO.add(dto);


        }
        return listDTO;
    }
}